﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using METU_CMT.Business.Compression;
using METU_CMT.Business.Report;
using METU_CMT.DataSets.METUCMTDataSetTableAdapters;

namespace METU_CMT.Business
{
    public class CourseInstructor : CourseTA
    {
        public CourseInstructor(string courseName, string courseCode, string semester)
        {
            CourseName = courseName;
            CourseCode = courseCode;
            Semester = semester;
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            int _courseId = 0;
            _courseTableAdapter.InsertCourse(courseName, courseCode, semester, out _courseId);
            courseId = _courseId;
            instructors = new List<Instructor>();
        }

        public override void GoToCourseMainPage(HttpResponse response)
        {
            HttpContext.Current.Session["MenuItem2"] = "~/InstructorCourseMainScreen.aspx";
            response.Redirect("~/InstructorCourseMainScreen.aspx");
            
        }

        public CourseInstructor(int courseId)
            : base(courseId)
        {
            //TODO: Not Implemented
        }

        public void AddInstructor(Instructor instructor)
        {
            instructors.Add(instructor);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertInstructor(courseId, instructor);
        }

        public void AddStudent(Student student)
        {
            students.Add(student);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertStudent(courseId, student.UserId);
        }

        //public void AddTA(TeachingAssistant ta)
        //{
        //    tas.Add(ta);
        //    CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
        //    _courseTableAdapter.InsertTA(courseId, ta);
        //}

        public void DeleteInstructor(Instructor instructor)
        {
            instructors.Remove(instructor);
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveInstructor(courseId, instructor);
        }

        public void DeleteStudent(Student student)
        {
            students.Remove(student);
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.RemoveStudent(courseId, student);
        }

        //public void DeleteTA(TeachingAssistant ta)
        //{
        //    tas.Remove(ta);
        //    CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
        //    _courseTableAdapter.RemoveTa(courseId, ta);
        //}

        public void UpdateInstructor(Instructor oldInstructor, Instructor newInstructor)
        {
            instructors.Remove(oldInstructor);
            instructors.Add(newInstructor);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.UpdateInstructor(courseId, oldInstructor, newInstructor);
        }

        //public void UpdateUser(Student oldStudent, Student newStudent)
        //{
        //    students.Remove(oldStudent);
        //    students.Add(newStudent);

        //    CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
        //    _courseTableAdapter.UpdateUser(courseId, oldStudent, newStudent);

        //}

        public void UpdateTA(TeachingAssistant oldTa, TeachingAssistant newTa)
        {
            tas.Remove(oldTa);
            tas.Add(newTa);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.UpdateTa(courseId, oldTa, newTa);

        }

        public void GetTeamWork(Event @event, out List<Deliverable> works, out List<Team> listOfTeams)
        {
            works = new List<Deliverable>();
            listOfTeams = new List<Team>();
            foreach (Team _team in teams)
            {
                works.Add(_team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == @event.EventId));
                listOfTeams.Add(_team);
            }
        }

        public void DownloadDocuments(Event @event)
        {
            CompressionAdapter _compressionAdapter = new CompressionAdapter();
            foreach (Team _team in teams)
            {
                _compressionAdapter.AddFile(
                    _team.Deliverables.FindLast( deliverable => deliverable.Event.EventId == @event.EventId ),
                    _team.TeamName );
            }
            _compressionAdapter.CompressAndDownloadDeliverables();
        }

        public void DownloadDocumentsOfTeam(Team team)
        {
            CompressionAdapter _compressionAdapter = new CompressionAdapter();
            foreach (Event _event in schedule)
            {
                _compressionAdapter.AddFile(
                    team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == _event.EventId), team.TeamName );
            }
            _compressionAdapter.CompressAndDownloadDeliverables();
        }


        public void DownloadSubmissionReport(Event @event)
        {
            ReportGenerator _reportGenerator = new ReportGenerator();
            foreach (Team _team in teams)
            {
                Deliverable _deliverable =
                    _team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == @event.EventId);
                if (_deliverable == null)
                {
                    _deliverable = new Deliverable { Event = @event };
                }
                ReportInfo _reportInfo = new ReportInfo(_team, _deliverable);
                _reportGenerator.AddReportInfo(_reportInfo);
            }
            _reportGenerator.CreateReportAndDownload();
        }

        public void AddTeamMember(int teamId, Student student)
        {
            Team _t = teams.Find(team => team.TeamId == teamId);
            _t.Members.Add(student);

            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            _courseTableAdapter.InsertTeamMember(teamId, student);

        }
    }
}